package com.example.plist.configure;



import com.google.gson.Gson;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class HttpClientRequest {

    public Map requestGet(String url, Map<String,String> params) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();

         List<String> list = new ArrayList<>();
        params.forEach((String k,String v)->{
            list.add(k+"="+v);
        });
        String p =String.join("&",list);

        if(!"".equals(p)||list.size()!=0){
            url=url+"?"+p;
        }

        HttpGet httpGet = new HttpGet(url);
        CloseableHttpResponse response = httpClient.execute(httpGet);
        HttpEntity entity = response.getEntity();
        String result = EntityUtils.toString(entity,"utf-8");
        Gson gson = new Gson();
        Map map = gson.fromJson(result,Map.class);
        response.close();
        httpClient.close();
        return map;
    }
    public void requestPost(String url,Map<String,String> params){

    }
}
